TestRiskCube <- function() {
  
  source("CalcDeltaArea.R")
  # Initialize Variables
  RecertSched = read.csv("RecertificationTotals.csv",header=FALSE, sep=",", quote="", dec=".",as.is=TRUE) #read in csv build from recertification schedule
  RecertSchedule <- as.integer(RecertSched) # transform Recert Schedule data to a vector of integers
  
#   PartProbsI = read.csv("PartProbs.csv",header=FALSE, sep=",", dec=".",,,)#read in part reliabilities from csv file
#   Reliability <- as.numeric(PartProbsI)  #transform reliability data to a numeric vector
#   PartProbs = 1 - Reliability  #define the failure rate as 1 - Reliability
#   PartProbs <- PartProbs[-1]  #truncate matrix to remove missile top level
#   PartProbs <- PartProbs[-1]  #truncate matrix to remove first stage level
  
  load("SM3_Demand.RData")
  PartProbs = 1-BNcK$Part$Probs[4:105]
  BNcKProb = BNcK$Prc[4:105] #initialize the Demand probability to be equal to the part reliability
  
  # We need to correct for unrealistic purchases, i.e. the guidance section
  xx = which(BNcKProb >PartProbs)
#   BNcKProb[xx] = PartProbs[xx]
  
  N = length(PartProbs)  #identify the total number of parts for use in the loop
  OpYears = length(RecertSchedule) #identify the number of years to run by using the recert schedule
  
  YearlyDemand = ceiling(outer(BNcKProb,RecertSchedule))  #simple calculation of number replaced rounded to next integer.  Can change this to distributed method
  #N by t vector has all predicted replacements for a given part along a single row
  
  # This part needs to change to reflect the fact that Purchases may go through a lot of different
  # variations as a result of user input, but for now it is what it is.
  alpha = 1
  InitialPurchase = 2
  Scale = 0
  RecertRate = 60
  BNcK_Purchases = CalcPurchase(alpha,BNcKProb,RecertSchedule,YearlyDemand,Scale)
  RLA_Purchases  = ceiling(outer(PartProbs,RecertSchedule))
  YearlyPurchases = RLA_Purchases
  RateDelta     = CalcDeltaArea(BNcKProb,      PartProbs)
  PurchaseDelta = CalcDeltaArea(BNcK_Purchases,RLA_Purchases)
  
  # Plot
  treeSort <- sort(BNcK$Part$Tree[4:105],index.return=TRUE)
  plot(BNcKProb[treeSort$ix],type="l",col="blue")
  lines(PartProbs[treeSort$ix],type="l",col="dark red")
  lines(treeSort$x*0.03/7,type="l",col="green",lwd=2)
  
  browser()
  
  source('CalcRiskCube.R')
  Risk <- CalcRiskCube(InitialPurchase,YearlyPurchases,YearlyDemand,RecertSchedule,RecertRate)
  return(Risk)
}
#=================================================================================================================================
